package org.opentcs.kcvehicle.communication.kc.udp.agv.param.function.navigation;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import org.opentcs.kcvehicle.communication.kc.common.byteutils.ByteUtils;

public class NavigationParam {
  //订单 ID,4个字节 ,Integer,从1开始依次累加
  private byte[] orderId;
  //任务 KEY,4个字节,Integer,从1开始依次累加
  private byte[] taskKey;
  //路径点信息个数（单条任务最大支持 8 个路径点）,1个字节 ,byte
  private byte pointNum;
  //路径信息个数（单条任务最大支持 8 个路径点）,1个字节,byte
  private byte pathNum;
  //保留,2个字节
  private byte[] retain;
  //任务中路径点信息结构体,长度 pointNum
  private Point[] points;
  //任务中路径信息结构体，长度 pathNum
  private Path[] paths;

  public NavigationParam(
      Integer orderId, Integer taskKey, byte pointNum, byte pathNum, Point[] points, Path[] paths
  ) {
    this.orderId = ByteUtils.intToBytes(orderId, 1);
    this.taskKey = ByteUtils.intToBytes(taskKey, 1);
    this.pointNum = pointNum;
    this.pathNum = pathNum;
    this.retain = new byte[]{(byte) 0x00, (byte) 0x00};
    this.points = points;
    this.paths = paths;
  }

  public byte[] toBytes() {
    ByteBuf byteBuf = Unpooled.buffer();
    byteBuf.writeBytes(orderId);
    byteBuf.writeBytes(taskKey);
    byteBuf.writeByte(pointNum);
    byteBuf.writeByte(pathNum);
    byteBuf.writeBytes(retain);

    for (Point point : points) {
      byteBuf.writeBytes(point.toBytes());
    }
    for (Path path : paths) {
      byteBuf.writeBytes(path.toBytes());
    }
    int i = byteBuf.writerIndex();
    byte[] bytes = new byte[i];
    byteBuf.readBytes(bytes);
    return bytes;
  }
}
